package com.hxqh.bo.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hxqh.bo.bean.FpsUserInfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * FpsUserInfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.hxqh.bo.bean.FpsUserInfo
 * @author MyEclipse Persistence Tools
 */

public class FpsUserInfoDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(FpsUserInfoDAO.class);
	// property constants
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String BROKERID = "brokerid";

	protected void initDao() {
		// do nothing
	}
	
	public List getHoldUser() {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql = "select distinct a.USER_ID,a.brokerid " +
						"from fps_user_info a, fps_user_policy_hold_info b " +
						"where a.user_id = b.USER_ID";
				SQLQuery q = arg0.createSQLQuery(sql);
				q.addScalar("USER_ID", Hibernate.INTEGER);
				q.addScalar("brokerid", Hibernate.INTEGER);
				return q.list();
			}
		});
	}

	public void save(FpsUserInfo transientInstance) {
		log.debug("saving FpsUserInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(FpsUserInfo persistentInstance) {
		log.debug("deleting FpsUserInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public FpsUserInfo findById(java.lang.Integer id) {
		log.debug("getting FpsUserInfo instance with id: " + id);
		try {
			FpsUserInfo instance = (FpsUserInfo) getHibernateTemplate().get(
					"com.hxqh.bo.bean.FpsUserInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(FpsUserInfo instance) {
		log.debug("finding FpsUserInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding FpsUserInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from FpsUserInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByBrokerid(Object brokerid) {
		return findByProperty(BROKERID, brokerid);
	}

	public List findAll() {
		log.debug("finding all FpsUserInfo instances");
		try {
			String queryString = "from FpsUserInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public FpsUserInfo merge(FpsUserInfo detachedInstance) {
		log.debug("merging FpsUserInfo instance");
		try {
			FpsUserInfo result = (FpsUserInfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(FpsUserInfo instance) {
		log.debug("attaching dirty FpsUserInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(FpsUserInfo instance) {
		log.debug("attaching clean FpsUserInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static FpsUserInfoDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (FpsUserInfoDAO) ctx.getBean("FpsUserInfoDAO");
	}
}